package com.huichenghe.xinlvsh01.zhy.utils;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.huichenghe.xinlvsh01.MainActivity;
import com.huichenghe.xinlvsh01.R;
import com.huichenghe.xinlvsh01.Utils.LocalDataSaveTool;
import com.huichenghe.xinlvsh01.Utils.MyConfingInfo;
import com.huichenghe.xinlvsh01.slide.EditPersionInfoActivity;
import com.huichenghe.xinlvsh01.slide.LoginActivity;

public class nextToLoginActivity extends AppCompatActivity
{
    private Button mLogin, mRegist, mDirect, nextStep;
    private LinearLayout regist_layout;
    private ImageView mImageView;
    private EditText pwd;
    private AutoCompleteTextView account;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next_to_login);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
//
//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });
        init();
    }


    /**
     * 初始化控件和设置监听
     */
    private void init()
    {
        account = (AutoCompleteTextView)findViewById(R.id.acount_for_regist);
        pwd = (EditText)findViewById(R.id.password_for_regist);
        nextStep = (Button)findViewById(R.id.next_step);
        nextStep.setOnClickListener(listener);
        mImageView = (ImageView)findViewById(R.id.regist_layout_back);
        regist_layout = (LinearLayout)findViewById(R.id.regist_layout);
        mLogin = (Button)findViewById(R.id.login_button);
        mRegist = (Button)findViewById(R.id.regist_button);
        mDirect = (Button)findViewById(R.id.direct_button);

        mImageView.setOnClickListener(listener);
        mLogin.setOnClickListener(listener);
        mRegist.setOnClickListener(listener);
        mDirect.setOnClickListener(listener);






    }


    /**
     * 设置监听
     */
    View.OnClickListener listener = new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            switch (v.getId())
            {
                case R.id.login_button:

                    Intent mIntent = new Intent();
                    mIntent.setClass(nextToLoginActivity.this, LoginActivity.class);
                    mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(mIntent);
                    nextToLoginActivity.this.finish();
                    break;

                case R.id.regist_button:
                    // 显示注册布局
                    showRegistLayout(mRegist, regist_layout, true);

                    break;
                case R.id.direct_button:
                    // 判断是否有缓存的个人信息
                    String info = LocalDataSaveTool.getInstance(nextToLoginActivity.this)
                            .readSp(MyConfingInfo.USER_NICK);
                    if(!info.isEmpty())  // 有缓存
                    {
                        Intent mmIntent = new Intent();
                        mmIntent.setClass(nextToLoginActivity.this, MainActivity.class);
                        mmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(mmIntent);
                    }
                    else                // 无缓存
                    {
                        // 进入EditPersionInfoActivity
                        Intent intent = new Intent();
                        intent.setClass(nextToLoginActivity.this, EditPersionInfoActivity.class);
                        intent.putExtra(MyConfingInfo.EDIT_OR_SHOW, MyConfingInfo.EDIT_PERSION_INFO);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                    nextToLoginActivity.this.finish();      // 结束当前页面
                    break;

                case R.id.regist_layout_back:
                    // 隐藏注册布局
                    showRegistLayout(mRegist, regist_layout, false);



                    break;

                case R.id.next_step:
                    checkTheEnterString();
                    break;

            }




        }
    };


    /**
     * 校验输入的内容
     */
    private void checkTheEnterString()
    {
        // Reset errors.
        account.setError(null);      // 复位错误提示
        pwd.setError(null);

        // Store values at the time of the login attempt.
        // 获取用户输入内容
        String accountt = account.getText().toString();
        String password = pwd.getText().toString();

        boolean cancel = false; // 代表没有通过验证
        View focusView = null;  // 没通过验证的view

        // Check for a valid password, if the user entered one.
        // 检查密码是否可用，
        if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
            pwd.setError(getString(R.string.error_invalid_password));
            focusView = pwd;
            cancel = true;
        }

        // 检查账号是否可用
        if (TextUtils.isEmpty(accountt)) {   // 非空验证
            account.setError(getString(R.string.error_field_required));
            focusView = account;
            cancel = true;
        }  else if (!isEmailValid(account)) {
            account.setError(getString(R.string.error_invalid_email));
            focusView = account;
            cancel = true;
        }

        if (cancel) {
            // There was an error; don't attempt login and focus the first
            // form field with an error.
            focusView.requestFocus();   // 没通过验证的获取焦点
        } else {
            // 进入完善资料页面
            Intent mIntent = new Intent();
            mIntent.setClass(nextToLoginActivity.this, EditPersionInfoActivity.class);
            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mIntent.putExtra(MyConfingInfo.EDIT_OR_SHOW, MyConfingInfo.EDIT_PERSION_INFO);
            mIntent.putExtra(MyConfingInfo.USER_ACCOUNT, accountt);
            mIntent.putExtra(MyConfingInfo.USER_PASSWORD, password);
            mIntent.putExtra(MyConfingInfo.REGIST, true);
            startActivity(mIntent);

        }

    }

    private boolean isEmailValid(AutoCompleteTextView account)
    {
        return account.length() >= 4;
    }

    private boolean isPasswordValid(String password)
    {
        return password.length() >= 6;
    }


    /**
     * 带动画的显示或隐藏注册布局
     * @param
     * @param mRegist
     * @param regist_layout
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void showRegistLayout(Button mRegist, final LinearLayout regist_layout, boolean show)
    {
        if(show)
        {
            // 执行圆形动画
            int[] location = new int[2];            // 两个元素的数组
            mRegist.getLocationOnScreen(location);
            int x = location[0] + (mRegist.getWidth()/2);                    // 圆心x轴
            int y = location[1];                    // 圆心y轴

            // get the final radius for the clipping circle
            int finalRadius = Math.max(regist_layout.getWidth(), regist_layout.getHeight());  // 最大半径
            int minRadius = mRegist.getWidth()/2;        // 最小半径

            // create the animator for this view (the start radius is zero)
            Animator anim =
                    ViewAnimationUtils.createCircularReveal(regist_layout, x, y, minRadius, finalRadius);

            // make the view visible and start the animation
            regist_layout.setVisibility(View.VISIBLE);
            anim.start();
        }
        else
        {

            int[] location = new int[2];
            mRegist.getLocationOnScreen(location);
            int cxx = location[0] + (mRegist.getWidth()/2);
            int cyy = location[1]/2;
//        int cyy = vg.getMeasuredHeightAndState();

//        int cxx = (int)vg.getX();
//        int cyy = (int)vg.getY();

            // get the initial radius for the clipping circle
            // 获取园的半径
            int initialRadiuss = regist_layout.getHeight();    // 最大半径
            int minRadiuss = regist_layout.getWidth()/2;             // 最小半径

            // create the animation (the final radius is zero)
            Animator anims =
                    ViewAnimationUtils.createCircularReveal(regist_layout, cxx, cyy, initialRadiuss, minRadiuss);

            // make the view invisible when the animation is done
            anims.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    regist_layout.setVisibility(View.GONE);
                }
            });
            // start the animation
            anims.start();


        }




    }


}
